package com.fangcun.service.admin.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangcun.bean.admin.agent.response.AgentGoodsResponse;
import com.fangcun.bean.admin.agent.response.AgentOrderResponse;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.entity.Agent;
import com.fangcun.entity.AgentGoods;
import com.fangcun.entity.AgentOrder;
import com.fangcun.entity.AgentShareBenefitsRule;
import com.fangcun.entity.User;
import com.fangcun.mapper.AgentGoodsMapper;
import com.fangcun.mapper.AgentMapper;
import com.fangcun.mapper.AgentOrderMapper;
import com.fangcun.mapper.AgentShareBenefitsRuleMapper;
import com.fangcun.mapper.UserMapper;
import com.fangcun.service.admin.AgentAdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class AgentAdminServiceImpl implements AgentAdminService {

	@Autowired
	private AgentMapper agentMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AgentGoodsMapper agentGoodsMapper;
	@Autowired
	private AgentOrderMapper agentOrderMapper;
	@Autowired
	private AgentShareBenefitsRuleMapper agentShareBenefitsRuleMapper;

	@Override
	public void update(Agent request) throws ProException {
		if (request.getUserId() != null) {
			/** 通过后- 更新用户 类型 **/
			User user = userMapper.selectByPrimaryKey(request.getUserId());
			user.setUserType(3);
			if (userMapper.updateByPrimaryKeySelective(user) <= 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
			/** 通过后- 创建一个 分利规则 **/
			AgentShareBenefitsRule rule = new AgentShareBenefitsRule();
			rule.setAgentId(request.getId());
			if (agentShareBenefitsRuleMapper.insertSelective(rule) <= 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
		if (agentMapper.updateByPrimaryKeySelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public Agent details(Long id) throws ProException {
		Agent request = agentMapper.selectByPrimaryKey(id);
		if (request == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}

	@Override
	public PageInfo<Agent> list(PageBean<Agent> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<Agent> list = agentMapper.listForAdmin(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public PageInfo<AgentGoodsResponse> goodsList(PageBean<AgentGoods> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<AgentGoodsResponse> list = agentGoodsMapper.listForAdminByAgentId(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public PageInfo<AgentOrderResponse> orderList(PageBean<AgentOrder> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<AgentOrderResponse> list = agentOrderMapper.listForAdminByAgentId(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}
}
